package com.lh.music.orm;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.lh.music.entity.Song;

import java.sql.SQLException;

/**
 * Created by a on 2016/12/22.
 */

public class OrmHelper extends OrmLiteSqliteOpenHelper {
    private static String DB_NAME="music.db";
    private static int DB_VERSION=1;
    private static OrmHelper ormHelper;
    //构造函数私有化，设计为单例设计模式
    private   OrmHelper(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }

    public OrmHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion) {
        super(context, databaseName, factory, databaseVersion);
    }
    public static synchronized OrmHelper getInstance(Context context){
        if (ormHelper==null){
            ormHelper=new OrmHelper(context);
        }
        return ormHelper;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        //创建表
        try {
            //创建喜欢音乐表
            TableUtils.createTable(connectionSource, Song.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {

    }
}
